package main

import (
	"fmt"
	"grpc-go-demo/echo"
	"log"
	"net"

	"google.golang.org/grpc"
)

type EchoImpl struct {
	echo.UnimplementedEchoServer
}

func (e *EchoImpl) ServerStreamEcho(req *echo.EchoRequest, stream echo.Echo_ServerStreamEchoServer) error {
	log.Println("received:", req.GetMessage())
	for i := 0; i < 100; i++ {
		err := stream.Send(&echo.EchoResponse{Message: req.GetMessage()})
		if err != nil {
			log.Fatal(err.Error())
			return err
		}
	}
	return nil
}

func main() {
	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln("open port error:", err.Error())
	}
	defer listener.Close()

	grpcServer := grpc.NewServer()

	echo.RegisterEchoServer(grpcServer, &EchoImpl{})

	fmt.Println("serverstream server start at port 8080:")

	grpcServer.Serve(listener)
}
